import { createRouter, createWebHistory } from 'vue-router'
import BasicLayout from '../layouts/BasicLayout.vue'
import { ElMessage } from 'element-plus'
import Cookies from 'js-cookie'

const routes = [
  {
    path: '/',
    component: BasicLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('../views/Home.vue')
      },
      {
        path: '/resources',
        name: 'ResourcesList',
        component: () => import('../views/resources/Index.vue'),
        meta: { title: '心理课堂' }
      },
      {
        path: '/consultation',
        name: 'Consultation',
        component: () => import('../views/consultation/Index.vue'),
        meta: { title: '在线咨询' }
      },
      {
        path: '/consultation/chat/:consultationId',
        name: 'Chat',
        component: () => import('@/views/consultation/chat.vue'),
        meta: { title: '咨询对话' }
      },
      {
        path: '/community',
        redirect: '/community/list',
        meta: { title: '心理社区' },
        children: [
          {
            path: 'list',
            name: 'ArticleList',
            component: () => import('@/views/community/ArticleList.vue'),
            meta: { title: '社区文章' }
          },
          {
            path: 'article/create',
            name: 'CreateArticle',
            component: () => import('@/views/community/ArticleEdit.vue'),
            meta: { title: '发布文章', activeMenu: '/community/list' }
          },
          {
            path: 'article/edit/:id',
            name: 'EditArticle',
            component: () => import('@/views/community/ArticleEdit.vue'),
            meta: { title: '编辑文章', activeMenu: '/community/list' }
          },
          {
            path: 'article/:id',
            name: 'ArticleDetail',
            component: () => import('@/views/community/ArticleDetail.vue'),
            meta: { title: '文章详情', activeMenu: '/community/list' }
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('@/views/admin/Layout.vue'),
    meta: { 
      requiresAuth: true  // 只需要登录验证
    },
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: () => import('../views/admin/Dashboard.vue'),
        meta: { requiresExpertOrAdmin: true }  // 改为允许专家访问
      },
      {
        path: 'users',
        component: () => import('@/views/admin/users/Index.vue'),
        meta: { title: '用户管理', requiresAdmin: true }
      },
      {
        path: 'scales',
        name: 'Scales',
        component: () => import('../views/admin/scales/Index.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'scales/:id/questions',
        name: 'ScaleQuestions',
        component: () => import('../views/admin/scales/Questions.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'assessments',
        name: 'Assessments',
        component: () => import('../views/admin/assessments/Index.vue'),
        meta: { requiresAdmin: true }
      },
      {
        path: 'consultations',
        name: 'Consultations',
        component: () => import('../views/admin/consultations/Index.vue'),
        meta: { 
          requiresAuth: true,
          requiresExpertOrAdmin: true,  // 允许专家或管理员访问
          title: '咨询记录'
        }
      },
      {
        path: 'resources',
        name: 'Resources',
        component: () => import('../views/admin/resources/Index.vue'),
        meta: { title: '教育资源管理', requiresAdmin: true }
      },
      {
        path: 'resources/create',
        name: 'ResourceCreate',
        component: () => import('../views/admin/resources/Edit.vue'),
        meta: { title: '新增教育资源', requiresAdmin: true }
      },
      {
        path: 'resources/edit/:id',
        name: 'ResourceEdit',
        component: () => import('../views/admin/resources/Edit.vue'),
        meta: { title: '编辑教育资源', requiresAdmin: true }
      }
    ]
  },
  {
    path: '/scale',
    component: BasicLayout,
    redirect: '/scale/list',
    children: [
      {
        path: 'list',
        component: () => import('../views/evaluation/Index.vue'),
        name: 'ScaleList',
        meta: { title: '心理测评', icon: 'scale' }
      },
      {
        path: 'eval/:id',
        component: () => import('../views/evaluation/Detail.vue'),
        name: 'ScaleEval',
        meta: { title: '量表测评', activeMenu: '/scale/list' },
        hidden: true
      },
      {
        path: 'result/:id',
        component: () => import('../views/evaluation/Result.vue'),
        name: 'ScaleResult',
        meta: { title: '测评结果', activeMenu: '/scale/list' },
        hidden: true
      }
    ]
  },
  {
    path: '/evaluation',
    name: 'Evaluation',
    component: () => import('../views/evaluation/Index.vue')
  },
  {
    path: '/evaluation/:id',
    name: 'EvaluationDetail',
    component: () => import('../views/evaluation/Detail.vue')
  },
  {
    path: '/evaluation/:id/result',
    name: 'EvaluationResult',
    component: () => import('../views/evaluation/Result.vue')
  },
  // 用户相关路由
  {
    path: '/user',
    component: BasicLayout,
    children: [
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('../views/user/Profile.vue'),
        meta: {
          title: '个人中心',
          requireAuth: true
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = Cookies.get('token')
  const userInfo = Cookies.get('userInfo') ? JSON.parse(Cookies.get('userInfo')) : null
  
  if (to.matched.some(record => record.meta.requiresAuth) && !token) {
    next('/login')
    ElMessage.warning('请先登录')
  } else if (to.matched.some(record => record.meta.requiresAdmin) && userInfo?.role !== 'ADMIN') {
    next('/')
    ElMessage.error('无权访问')
  } else if (to.matched.some(record => record.meta.requiresExpertOrAdmin) && !['ADMIN', 'EXPERT'].includes(userInfo?.role)) {
    next('/')
    ElMessage.error('无权访问')
  } else {
    next()
  }
})

export default router 